package com.example.bakend.controller;

import com.example.bakend.pojo.Book;
import com.example.bakend.pojo.Result;
import com.example.bakend.service.BookService;
import com.example.bakend.service.CategoryService;
import com.example.bakend.service.RecordService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("book")
public class BookController {
    @Autowired
    BookService bookService;
    @Autowired
    RecordService recordService;
    @Autowired
    CategoryService categoryService;

    @GetMapping
    public Result selectBooks(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize) {
        return Result.success(bookService.selectBooks(pageNum, pageSize));
    }

    @GetMapping("{bookId}")
    public Result selectBook(@PathVariable Integer id) {
        return Result.success(bookService.selectBook(id));
    }

    @PostMapping
    public Result insertBook(@RequestBody Book book) {
//        查询校验名称是否重复
        List<Book> books = bookService.selectBooks();
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i).getTitle().equals(book.getTitle())) {
                return Result.error("名称不能重复");
            }
        }
//        库存校验
        if (book.getStockQuantity() < 0) {
            return Result.error("库存不合法");
        }
//        分类ID校验
        if (book.getCategoriesId() < 0) {
            return Result.error("分类ID不合法");
        }
//        时间校验
        if (book.getPublishDate().after(new Date())) {
            return Result.error("出版日期不合法");
        }

//        校验分类
        if (book.getCategoriesId() > categoryService.getAll().size()){
            return Result.error("分类不存在");
        }

        return Result.success(bookService.insertBook(book));
    }

    @PutMapping
    public Result updateBook(@RequestBody Book book) {

        List<Book> books = bookService.selectBooks();
        for (int i = 0; i < books.size(); i++) {
//            左边是标题不能重复     右边是跳过当前book的校验，因为可以不修改当前title，没有的话不修改会报错
            if (books.get(i).getTitle().equals(book.getTitle()) && !books.get(i).getId().equals(book.getId())) {
                return Result.error("标题不能重复");
            }
        }
        //        库存校验
        if (book.getStockQuantity() < 0) {
            return Result.error("库存不合法");
        }
//        分类ID校验
        if (book.getCategoriesId() < 0) {
            return Result.error("分类ID不合法");
        }
//        时间校验
        if (book.getPublishDate().after(new Date())) {
            return Result.error("出版日期不合法");
        }

        if (book.getCategoriesId() > categoryService.getAll().size()){
            return Result.error("分类不存在");
        }


        return Result.success(bookService.updateBook(book));
    }


    @GetMapping("tiaojian")
    public Result getRecords(@RequestParam(name = "title", required = false) String title,
                             @RequestParam(name = "author", required = false) String author,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime publishDateBegin,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime publishDateEnd,
                             @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize) {
        return Result.success(bookService.getBooks(title, author, publishDateBegin, publishDateEnd, pageNum, pageSize));
    }


    @DeleteMapping("{ids}")
    public Result deleteBooks(@PathVariable List<Integer> ids) {
//        删除book校验是否已经借阅，已经借阅不支持删除book
//        for (int i = 0; i < ids.size(); i++) {
//            int bookId = ids.get(i);
//            Object record = recordService.selectBookIdRecord(bookId);
//            System.out.println("Checking book ID: " + bookId + ", Record: " + record);
//            if (record != null){
//                return Result.error("该书已被借阅，不支持删除");
//            }
//        }



        return Result.success(bookService.deleteBooks(ids));
    }


    @GetMapping("fenlei")
    public Result getBooksForCategory(@RequestParam(name = "categoriesId",required = false) Integer categoriesId){

        return Result.success(bookService.getBooksForCategory(categoriesId));
    }


}
